<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserCivil;
use think\Db;

/**公考课管理 */
class Civil extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**公考检查是否需要弹窗 */
    public function checkPopup()
    {
        $user_id = $this->auth->id;
        if (!$user_id) {
            return $this->error('用户未登录');
        }
        // 获取用户所有有效的公考资源
        $user_civils = UserCivil::getUserValidCivils($user_id);
        if ($user_civils && count($user_civils) > 0) {
            // 有有效公考资源，需要弹窗
            // $civil_data = [];
            // foreach ($user_civils as $civil) {
            //     $civil_data[] = [
            //         'id' => $civil['id'],
            //         'civil_id' => $civil['civil_id'],
            //         'teacher_id' => $civil['teacher_id'],
            //         'end_time' => $civil['end_time'],
            //         'end_time_text' => $civil->end_time_text,
            //         'is_expired' => $civil->is_expired,
            //         'createtime' => $civil['createtime'],
            //         'createtime_text' => date('Y-m-d H:i:s', $civil['createtime'])
            //     ];
            // }
            // return $this->success('不需要弹窗', [
            //     'need_popup' => false,
            //     'message' => '您有' . count($user_civils) . '个可用的公考资源',
            //     'total_count' => count($user_civils),
            //     'data' => $civil_data
            // ]);
            $need_popup  = false;
        } else {
            $need_popup  = true;
            // 没有有效公考资源，不需要弹窗
            // return $this->success('需要弹窗', [
            //     'need_popup' => true,
            //     'message' => '您暂无可用的公考资源',
            //     'total_count' => 0,
            //     'data' => ['civil_pic' => ];
            // ]);
        }
        $this->success("OK", [
            'need_popup' => $need_popup,
            'data' => ['civil_pic' => config('site.civil_pic'), 'civil_con' => config('site.civil_con')]
        ]);
    }

    /**
     * 检查用户是否有即将过期的公考资源
     */
    public function list()
    {
        $user_id = $this->auth->id;
        $list = (new UserCivil())->getUserCivilList($user_id);
        $this->success("OK", [
            'list' => $list
        ]);
    }

    /**
     * 公考详情
     * @return void 
     */
    public function detail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error("ID不能为空");
        }
        $info = (new UserCivil())->getUserCivilDetails($id);
        if (!$info) {
            $this->error("公考不存在");
        }
        $this->success("OK", [
            'info' => $info
        ]);
    }
    
}
